/*
 * Proto Streamer
 * Copyright (C) 2010 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.s;

import java.io.IOException;


/**
 * @author jdp
 */
public abstract class Resource
    extends Abstract
{
    protected final static long LastModified = (System.currentTimeMillis()/Seconds)*Seconds;

    public static class Get
        extends Resource
    {
        public Get(String prefix){
            super(prefix);
        }

        public void init(Request request)
            throws IOException
        {
            super.init(request);
            if (0 < this.resourcelen){

                this.setStatusOk();
                this.setConnection(request);

                Headers headers = this.headers;
                headers.setContentTypeFor(this.path);
                headers.setContentLength(this.resourcelen);
                headers.setLastModified(LastModified);
                headers.setETagFor(LastModified,this.resourcelen);


                if (this.isModifiedSince(request))
                    this.setStatusOk();
                else
                    this.setStatusNotModified();
            }
            else if (null == Mimetype.For(this.path)){

                this.redirect(request,"index.html");
            }
            else
                this.setStatusNotFound();
        }

        public void tail(Request request, Output out)
            throws IOException
        {
            if (this.isOk()){
                out.write(this.resource,0,this.resourcelen);
                out.flush();
            }
        }
    }

    protected final String prefix;

    protected String path;

    protected byte[] resource;

    protected int resourcelen;


    public Resource(String prefix){
        super();
        this.prefix = prefix;
    }



    public boolean isModifiedSince(Request request){

        long since = request.getIfModifiedSince();
        if (0L < since){
            
            return (since < LastModified);
        }
        return true;
    }
    public void init(Request request)
        throws IOException
    {
        super.init(request);
        this.setLocation(request);

        this.path = alto.io.u.Chbuf.fcat(this.prefix, request.getPath().getPath());

        this.resource = Resources.For(this.path);
        if (null == this.resource)
            this.resource = new byte[0];

        this.resourcelen = this.resource.length;
    }

}
